import React, { Component } from 'react';
import CustomForm from '../../../components/CustomForm';
import { message, Select } from 'antd';
import services from '../../../services/api';

const Option = Select.Option;

class Distribute extends Component {
  constructor(props) {
    super(props);
    this.form = null;
    this.api = 'caseApi';
    this.state = {
      employees: []
    };
    this.actionType = this.props.actionType || 'distribute'
    this.saveData = this.props.saveData || {} // 案件详情里当前分配人流转到其他分配人时需保存当前的修改
    this.event_id = this.props.event_id
  }

  componentWillMount() {
    this.getEmployees();
  }

  // 获取分配人员列表
  getEmployees = () => {
    const params = {
      case_ids: this.props.case_id,
      event_id: this.event_id,
      is_need_self: this.actionType === 'distribute' ? true : false
    }
    services[this.api].getEmployees(params).then(res => {
      if (res.code === 0) {
        this.setState({
          employees: res.data
        });
      } else {
        message.error(res.msg);
      }
    });
  };

  handleSubmit = () => {
    this.form.validateFields((err, values) => {
      if (!err) {
        const params = {
          user_id: values.user_id,
        };
        let action = ''
        switch (this.actionType) {
          case 'distribute':
            action = 'distributeCase'
            params.case_ids = this.props.case_id
            break
          case 'distribute_and_save':
            action = 'distributeAddSave'
            params.case_id = this.props.case_id
            break
          default: break
        }
        services.caseApi[action]({ ...params, ...this.saveData }).then(data => {
          if (data.code === 0) {
            message.success(data.msg);
            this.props.clearSelected && this.props.clearSelected();
            if (this.props.closeModal) {
              this.props.closeModal();
            }
          } else {
            message.error(data.msg);
          }
        });
      }
    });
  };

  handleConfirmBlur = e => {
    const { value } = e.target;
    this.setState({ confirmDirty: this.state.confirmDirty || !!value });
  };

  render() {
    const formItems = [
      {
        label: {
          label: '审核人员',
          className: 'tantuer-form-item3 tantuer-form-item-nowrap'
        },
        name: 'user_id',
        span: 24,
        options: {
          initialValue: '',
          rules: [{ required: true, message: '请选择' }]
        },
        component: (
          <Select
            placeholder='请选择'
            showSearch
            dropdownStyle={{ maxHeight: 400 }}
            optionFilterProp='children'
          >
            {this.state.employees.map(item => {
              return (
                <Option value={item.user_id} key={item.user_id}>
                  {item.user_name}
                </Option>
              );
            })}
          </Select>
        )
      },
    ];

    return (
      <CustomForm
        colNum={1}
        closeModal={() => this.props.closeModal(true)}
        createForm={ref => (this.form = ref)}
        formItems={formItems}
        onHandleSubmit={this.handleSubmit}
      />
    );
  }
}

export default Distribute;
